package com.socialuni.center.web.serive.comment;

import com.socialuni.sdk.api.model.QO.comment.UniCommentPostQO;
import com.socialuni.sdk.api.model.RO.comment.TalkCommentRO;
import com.socialuni.center.web.utils.CenterUserUtil;
import com.socialuni.center.web.utils.UnionIdDbUtil;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.QO.comment.CommentAddVO;
import com.socialuni.sdk.service.comment.SocialPostCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author qinkaiyuan
 * @date 2020-05-16 20:11
 */
@Service
@Slf4j
public class CenterPostCommentService {
    @Resource
    SocialPostCommentService socialPostCommentService;


    public TalkCommentRO postComment(@RequestBody @Valid @NotNull UniCommentPostQO addVO) throws IOException {
        UserDO user = CenterUserUtil.getMineUser();
        //推送消息
//        notifyService.sendNotifies(comment.getNotifies());
        //因前台问题，无论如何都返回父级comment
        /*if (comment.getParentComment() != null) {
            comment = commentRepository.findById(comment.getParentComment().getId()).get();
        }*/
        CommentAddVO commentAddVO = new CommentAddVO();

        commentAddVO.setContent(addVO.getContent());

        Integer talkIdResult = UnionIdDbUtil.getTalkIdByUnionId(addVO.getTalkId(), user);
        commentAddVO.setTalkId(talkIdResult);

        String commentId = addVO.getCommentId();
        //不为null，才查询
        if (commentId != null) {
            Integer commentIdResult = UnionIdDbUtil.getCommentIdByUid(commentId, user);
            commentAddVO.setCommentId(commentIdResult);

            String replyCommentId = addVO.getReplyCommentId();
            if (replyCommentId != null) {
                Integer replyCommentIdResult = UnionIdDbUtil.getCommentIdByUid(replyCommentId, user);
                commentAddVO.setReplyCommentId(replyCommentIdResult);
            }
        }

        return socialPostCommentService.addComment(commentAddVO, user, addVO.getThreeUnionId());
    }

}
